package com.ruoyi.school.homework.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.school.homework.domain.HomeworkProblem;
import com.ruoyi.school.homework.mapper.HomeworkMapper;
import com.ruoyi.school.homework.domain.Homework;
import com.ruoyi.school.homework.service.IHomeworkService;
import com.ruoyi.common.core.text.Convert;

/**
 * 作业Service业务层处理
 * 
 * @author qiang
 * @date 2022-07-10
 */
@Service
public class HomeworkServiceImpl implements IHomeworkService 
{
    @Autowired
    private HomeworkMapper homeworkMapper;

    /**
     * 查询作业
     * 
     * @param id 作业主键
     * @return 作业
     */
    @Override
    public Homework selectHomeworkById(Long id)
    {
        return homeworkMapper.selectHomeworkById(id);
    }

    /**
     * 查询作业列表
     * 
     * @param homework 作业
     * @return 作业
     */
    @Override
    public List<Homework> selectHomeworkList(Homework homework)
    {
        return homeworkMapper.selectHomeworkList(homework);
    }

    /**
     * 新增作业
     * 
     * @param homework 作业
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHomework(Homework homework)
    {
        int rows = homeworkMapper.insertHomework(homework);
        insertHomeworkProblem(homework);
        return rows;
    }

    /**
     * 修改作业
     * 
     * @param homework 作业
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHomework(Homework homework)
    {
        homeworkMapper.deleteHomeworkProblemByHomeworkId(homework.getId());
        insertHomeworkProblem(homework);
        return homeworkMapper.updateHomework(homework);
    }

    /**
     * 批量删除作业
     * 
     * @param ids 需要删除的作业主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHomeworkByIds(String ids)
    {
        homeworkMapper.deleteHomeworkProblemByHomeworkIds(Convert.toStrArray(ids));
        return homeworkMapper.deleteHomeworkByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除作业信息
     * 
     * @param id 作业主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHomeworkById(Long id)
    {
        homeworkMapper.deleteHomeworkProblemByHomeworkId(id);
        return homeworkMapper.deleteHomeworkById(id);
    }

    /**
     * 新增作业题目信息
     * 
     * @param homework 作业对象
     */
    public void insertHomeworkProblem(Homework homework)
    {
        List<HomeworkProblem> homeworkProblemList = homework.getHomeworkProblemList();
        Long id = homework.getId();
        if (StringUtils.isNotNull(homeworkProblemList))
        {
            List<HomeworkProblem> list = new ArrayList<HomeworkProblem>();
            for (HomeworkProblem homeworkProblem : homeworkProblemList)
            {
                homeworkProblem.setHomeworkId(id);
                list.add(homeworkProblem);
            }
            if (list.size() > 0)
            {
                homeworkMapper.batchHomeworkProblem(list);
            }
        }
    }
}
